import { Component,OnInit,HostListener,ElementRef, ViewChild  } from '@angular/core';
import {Store,Select} from '@ngxs/store';
import {SetNav,UserGuidState} from '../app.state';
import {Observable} from 'rxjs';
import {RollService} from '../my-service/roll.service';
import {UtilService} from '../my-service/util.service';
import { ActivatedRoute } from '@angular/router';
import {CodeService} from '../my-service/code.service';

@Component({
  selector: 'app-roster-info',
  templateUrl: './roster-info.component.html',
  styleUrls: ['./roster-info.component.css']
})
export class RosterInfoComponent implements OnInit  {
  @Select(UserGuidState) userGuid$: Observable<any>; //获取用户guid和登录状态
  userGuid:string;
  classGuid:string;
  bodyWidth=0;//当前屏幕宽度
  activeIndex=0; //当前元素
  total =2;//总共能够滑动的张数
  // itemPossion=[];
  classInfo={
    ClassName:'',
    CreateTime:'',
    EndTime:'',
    Creator:'',
    Number:0,
  };
  touchStartCoord={
    clientX:0,
    clientY:0
  }; // 触摸开始坐标
  touchEndCoord={
    clientX:0,
    clientY:0
  }; //触摸移动中坐标
  pushs=[];
  timePush=[];// 将推送按照时间分组
  notices=[];//公告
  @HostListener('touchstart')
  touchStart(){
    // 获取起始滑动坐标
    this.touchStartCoord.clientX=event['targetTouches'][0].clientX;
    this.touchStartCoord.clientY=event['targetTouches'][0].clientY;
  }
  @HostListener('touchmove')
  touchMove(){
  }
  @HostListener('touchend')
  touchEnd(){
    //获取结束时的坐标
    this.touchEndCoord.clientX=event['changedTouches'][0].clientX;
    this.touchEndCoord.clientY=event['changedTouches'][0].clientY;
    //滑动移动方向 左：-x,右+x;上-y,下+y
    if(Math.abs(this.touchEndCoord.clientY-this.touchStartCoord.clientY)>=20){
      //上下滑动
    }else {
      // 左右滑动，左滑下一张，右滑上一张
      let distance = this.touchEndCoord.clientX-this.touchStartCoord.clientX;
      let distanceABS = Math.abs(distance);
      if(distanceABS>=this.bodyWidth*0.05){
        //有效滑动
        if(distance<0){
          //左滑
          if(this.activeIndex==this.total-1){
            window.alert('当前页面为最后一张页面，请向右滑动');
          }else {
            // this.itemPossion[this.activeIndex]-=this.bodyWidth;
            // this.itemPossion[this.activeIndex-1]+=this.bodyWidth;
            this.activeIndex+=1;
          }
        }else {
          //右滑
          if(this.activeIndex==0){
            window.alert('当前页面为第一张页面，请向左滑动');
          }else {
            // this.itemPossion[this.activeIndex]+=this.bodyWidth;
            // this.itemPossion[this.activeIndex+1]-=this.bodyWidth;
            this.activeIndex-=1;
          }
        }
      }

    }
  }
  constructor(
    private store:Store,
    private rollService:RollService,
    private utilService:UtilService,
    private router:ActivatedRoute,
    private codeService:CodeService
  ) {
    this.store.dispatch(new SetNav('roster'));
    this.userGuid$.subscribe(res=>{
      this.userGuid=res.userGuid;
    })
  }
  //获取推送
  getPushs(classGuid:string){
    this.codeService.getClassPushsByDay(classGuid).then(res=>{
      if(res.status==1){
        let arr = [];
        for(let i in res.data){
          arr.push(res.data[i]);
        }
        // 将数组翻转
        arr.reverse();
        //过滤掉和班级本身推送
        for(let i = 0;i<arr.length;i++){
             for(let j =0;j<arr[i].length;j++){
               let  time =new Date(arr[i][j].CreateTime*1000);
               arr[i][j].CreateTime=this.utilService.changDateStringOfDay(time);
               if(arr[i][j].Code.substr(0,2)=='sr'){
                 arr[i].splice(j,1);
                 j--;
               }
             }
        }
        //过滤掉空数组
        arr.forEach((item,index)=>{
           if(item.length==0){
             arr.splice(index,1);
           }
        });
        this.pushs=arr;
      }
    })
  }
  //获取公告
  getNotices(classGuid,userGuid){
    this.rollService.getNoticeList(classGuid,userGuid).then(res=>{
      if(res.status==1){
        if(res.data[0]){
            res.data.forEach(notice=>{
              let date =new Date(notice.PublicTime);
              notice.PublicTime = this.utilService.changDateStringOfDay(date);
            });
          this.notices=res.data;
        }
      }
    })
  }
  ngOnInit(){
  this.bodyWidth  = document.body.clientWidth;
    // this.itemPossion[0]=0;
    // this.itemPossion[1]=this.bodyWidth;
    this.classGuid = this.router.snapshot.params["id"];
    //获取班级信息
    this.rollService.getClassList('"'+this.classGuid+'"').then(res=>{
      if(res.status==1){
        this.classInfo = res.data[0];
      }
    });
    this.getPushs(this.classGuid);
    this.getNotices(this.classGuid,this.userGuid);
  }
  // 跳转到其他应用
  goApp(push){
    window.location.href=push.ClientAddress;
  }
}
